home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / ACORNUSERS / EMULATOR / BABE / !Babe / asm (.txt) < prev    next >
RISC OS BBC BASIC V Source  |  1998-02-04  |  7KB  |  323 lines

  1.  Project Commenced: 13/1/98
  2.  log%=1 
  3. Assembler(infile$,outfile$)
  4.  open file and set up variables
  5. outfile$,
  6. (outfile$)-1,1)<>"." 
  7. ,   logfile$=
  8. outfile$,
  9. (outfile$)-1)+"L"
  10.    logfile$=outfile$+"L"
  11.  log%=1 
  12.  logfile=
  13. (logfile$)
  14.  log%=1 
  15. #logfile,"Logfile of assembly of: "+infile$
  16.  log%=1 
  17. #logfile,"Saved as: "+outfile$
  18.  log%=1 
  19. #logfile,version$
  20.  log%=1 
  21. #logfile,
  22. infile=
  23. (infile$)
  24. )asm_error%=0  :
  25.  assembly error found
  26. asm_erl%=0
  27. 5asmlines%=0 :
  28.  number of lines actually assembled
  29. 'object%=0 :
  30.  current assembled line
  31. 3numval%=0 :
  32.  value for NUM instr (for assembly)
  33. -   asm$=
  34. #infile   :
  35.  read line from file
  36.        
  37. 1   count%=1           :
  38.  init string position
  39. @   count%=
  40. FindNoneSpace(asm$,count%) :
  41.  skip leading spaces
  42. asm$,count%,1))<>-1 
  43.  asm_erl%+=1  :
  44.  if not CR then inc line
  45.       
  46.  G   
  47.  the next line checks for any CRs which read as a separate line
  48. !J   
  49.  and comments which should be the 1st thing or last thing on line 
  50. "8   
  51. asm$,count%,1)<>";" 
  52. asm$,count%,1))<>-1 
  53.       
  54.  count%<>0 
  55. $E         storeline$=
  56. asm$,count%,2) :
  57.  all storeline are 2 digits
  58. %D         
  59.  storeline$<>"00" 
  60. (storeline$)=0 
  61.  asm_error%=1:
  62. &D         
  63. (storeline$)>31 
  64. (storeline$)<0 
  65.  asm_error%=3:
  66. '9         count%+=2 :
  67.  place pointer after line number
  68.       
  69. *"      
  70.  get next nonspace char
  71. +;      count%=
  72. FindNoneSpace(asm$,count%) :
  73.  skip spaces
  74. ,?      
  75.  if none found then only a line store number - error
  76. -'      
  77.  count%=0 
  78.  asm_error%=4:
  79. /&      
  80.  check for comment mid-line
  81. 02      
  82. asm$,count%,1)=";" 
  83.  asm_error%=2:
  84.             
  85. 27      
  86.  Check that not prematurely at end of string
  87. 3.      
  88.  count%>=
  89. (asm$) 
  90.  asm_error%=5:
  91. 5A      
  92.  Check mnemonic, allowing lower case or mixed case too
  93. 6C      mnemonic1$=
  94. asm$,count%,3) :
  95.  all mnemonics are 3 letters
  96.       mnemonic$=""
  97. 8)      
  98.  Convert mnemonic to uppercase
  99.       
  100.  x%=1 
  101. :&         char=
  102. mnemonic1$,x%,1))
  103. ;3         
  104.  char>90 
  105.  char-=32 :
  106.  make uppercase
  107.          mnemonic$+=
  108. (char)
  109.       
  110. >*      
  111.  assemble mnemonic -> binary   
  112.       
  113.  mnemonic$ 
  114. @          
  115.  "JMP" : fn$="000"
  116. A          
  117.  "JRP" : fn$="100"
  118. B          
  119.  "LDN" : fn$="010"
  120. C          
  121.  "STO" : fn$="110"
  122. D          
  123.  "SUB" : fn$="001"
  124. E<         
  125.  "SB5" : 
  126.  asmopt%?0=1 
  127.  fn$="001" 
  128.  fn$="101"
  129. F          
  130.  "CMP" : fn$="011"
  131. G          
  132.  "STP" : fn$="111"
  133. HF         
  134.  "NUM" : fn$="XXX"   :
  135.  put 32 bit signed number on line
  136. I4         
  137.   : asm_error%=6    :
  138.  unrecognised fn
  139.       
  140.       
  141.  asm_error%=6 
  142. LB      count%+=3 :
  143.  increment position to letter after mnemonic
  144. MG      
  145.  mnemonic$="STP" 
  146.  mnemonic$="CMP" 
  147.  actlreq%=
  148.  actlreq%=
  149. O"      
  150.  get next nonspace char
  151. P;      count%=
  152. FindNoneSpace(asm$,count%) :
  153.  skip spaces
  154. R1      
  155.  since STP and CMP requires no actline
  156.       
  157.  actlreq% 
  158. T@         
  159.  only a line store number and mnemonic no act line
  160. U*         
  161.  count%=0 
  162.  asm_error%=8:
  163. VE         
  164.  no comments mid line after mnemonic and before actline
  165. W5         
  166. asm$,count%,1)=";" 
  167.  asm_error%=7:
  168.       
  169.       
  170.  get actline
  171.       countn%=count%
  172.       
  173.          countn%+=1
  174. ^#         char$=
  175. asm$,countn%,1)
  176. _J         
  177. "0123456789 ;"+
  178. 13,char$)=0 
  179.  actlreq% 
  180.  asm_error%=10
  181. `_      
  182.  char$=" " 
  183. (char$)=10 
  184. (char$)=13 
  185.  char$=";" 
  186.  countn%>=
  187. (asm$) 
  188.  asm_error%>0
  189.       
  190.  asm_error%>0 
  191. b3      actline$=
  192. asm$,count%,(countn%-count%)+1)
  193. d+      
  194.  check for out of range actlines
  195. eR      
  196.  mnemonic$<>"NUM" 
  197. (actline$)>31 
  198. (actline$)<0) 
  199.  asm_error%=9:
  200. f]      
  201.  log%=1 
  202. #logfile,"SL: "+storeline$+" FN: "+mnemonic$+" AL: "+actline$+"      ";
  203.       
  204.  Assemble code
  205. i<      asmlines%+=1      :
  206.  inc number of lines assembled
  207. j+      obj$(object%)="00"+storeline$+":"
  208.       
  209.  mnemonic$<>"NUM" 
  210. l+         actlineb$=actlin$(
  211. (actline$))
  212. m%         obj$(object%)+=actlineb$
  213. n#         
  214.  actlineb$<>"00000" 
  215. o,            
  216.  mnemonic$="CMP" 
  217. Warn(1)
  218. p,            
  219.  mnemonic$="STP" 
  220. Warn(2)
  221.          
  222. r0         obj$(object%)+=
  223. 8,"0")+fn$+
  224. 16,"0")
  225. s:         
  226.  add spare 8 bits, fn and next spare 16 bits
  227.       
  228. u"         
  229.  set R1 for assembly
  230.          B%=
  231. (actline$)
  232. w$         numval%=
  233. (Reverse32Bit)
  234. xF         
  235.  "OS_ConvertBinary4",numval%,binstring%,33 
  236.  ,A%:?A%=13
  237. y6         obj$(object%)=obj$(object%)+$(binstring%)
  238.       
  239. {,      
  240.  log%=1 
  241. #logfile,obj$(object%)
  242.       object%+=1
  243. #infile 
  244.  asm_error%>0
  245. #infile
  246.  log%=1 
  247. #logfile,"Saving Object code file..."
  248.  output assembled file
  249. outfile=
  250. (outfile$)
  251. #outfile,
  252. asmlines%    :
  253.  for compatiblity with PC!
  254.  x%=0 
  255.  asmlines%-1 
  256.       
  257. #outfile,obj$(x%)
  258.  this automatically outputs an extra line just like PC
  259. #outfile
  260. ("SETTYPE "+outfile$+" BAB")
  261.  asm_error%<>0 
  262.  display error
  263.  asm_error% 
  264. '      
  265.  1  : m$="Invalid storeline"
  266. Q      
  267.  2  : m$="Cannot have a comment after a storeline but before mnemonic"
  268. J      
  269.  3  : m$="Store line cannot be greater than 31 or less than 00"
  270. B      
  271.  4  : m$="Only a line store, no mnemonic, nor act line"
  272. 9      
  273.  5  : m$="Incomplete mnemonic and no act line"
  274. +      
  275.  6  : m$="Unrecognised mnemonic"
  276. 7      
  277.  7  : m$="Comments are not allowed mid line"
  278. J      
  279.  8  : m$="All mnemonics except CMP and STP require an act line"
  280. Q      
  281.  9  : m$="Actlines must be in the range 0-31 except for with a NUM fn"
  282. L      
  283.  10 : m$="Actlines must be an integer value only using digit 0-9"
  284.  log%=1 
  285. #logfile,m$+" at line "+
  286. (asm_erl%)+" ptr="+
  287. count%
  288.  OSCLI("SPOOL")
  289.  log%=1 
  290. #logfile,"Assembly complete."
  291.  log%=1 
  292. #logfile
  293.  log%=1 
  294. ("SETTYPE "+logfile$+" FFF")
  295.  data for actline conversion from number 0 to 31
  296.  "00000","10000","01000","11000","00100","10100","01100","11100","00010"
  297.  "10010","01010","11010","00110","10110","01110","11110","00001","10001"
  298.  "01001","11001","00101","10101","01101","11101","00011","10011","01011"
  299.  "11011","00111","10111","01111","11111"
  300. Warn(warn%)
  301.  warn% 
  302. N      
  303.  1 : w$="CMP instructions shouldn't really have an actline applied"
  304. N      
  305.  2 : w$="STP instructions shouldn't really have an actline applied"
  306.  log%=1 
  307. #logfile,"WARNING: "+w$+" at line "+
  308. (asm_erl%)+" ptr="+
  309. count%
  310. FindNoneSpace(asm$,count%)
  311.  pass to it string to be checked, and char to start checking from
  312.  passes back pos of next non string char
  313.    notspace%=
  314. asm$,count%,1)=" " 
  315.  count%<
  316. (asm$)
  317.       count%+=1
  318. asm$,count%,1)=" " 
  319.  count%=
  320. (asm$) 
  321.  count%=0
  322. =count%
  323.